home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / hdf / hdf.lha / DFGROUP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1980-02-06  |  7.7 KB  |  251 lines

  1. /***************************************************************************
  2. *
  3. *
  4. *                         NCSA HDF version 3.2r2
  5. *                            October 30, 1992
  6. *
  7. * NCSA HDF Version 3.2 source code and documentation are in the public
  8. * domain.  Specifically, we give to the public domain all rights for future
  9. * licensing of the source code, all resale rights, and all publishing rights.
  10. *
  11. * We ask, but do not require, that the following message be included in all
  12. * derived works:
  13. *
  14. * Portions developed at the National Center for Supercomputing Applications at
  15. * the University of Illinois at Urbana-Champaign, in collaboration with the
  16. * Information Technology Institute of Singapore.
  17. *
  18. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  19. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  20. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  21. *
  22. ****************************************************************************
  23. */
  24.  
  25. #ifdef RCSID
  26. static char RcsId[] = "@(#)$Revision: 1.1 $";
  27. #endif
  28. /*
  29. $Header: /hdf/hdf/v3.2r2/src/RCS/dfgroup.c,v 1.1 1992/08/25 21:40:44 koziol beta koziol $
  30.  
  31. $Log: dfgroup.c,v $
  32.  * Revision 1.1  1992/08/25  21:40:44  koziol
  33.  * Initial revision
  34.  *
  35. */
  36. /*-----------------------------------------------------------------------------
  37.  * File:    dfgroup.c
  38.  * Purpose: Low level functions for implementing groups
  39.  * Invokes: df.c df.h
  40.  * Contents:
  41.  *  DFdiread: read in the data identifier list from the group
  42.  *  DFdiget: get next data identifier from list
  43.  *  DFdisetup: get ready to store a list of data identifiers to write out
  44.  *  DFdiput: add a data identifier to the list to be written out
  45.  *  DFdiwrite: write out the list of data identifiers
  46.  *  DFDIputgroup: write out a group (array of tag/refs)
  47.  *  DFDIgetgroup: read in a group (array of tag/refs)
  48.  * Remarks: A group is a way of associating data elements with each other.
  49.  *          It is a tag whose data is a list of tag/refs
  50.  *          Each tag/ref combination is called a data identifier (DI).
  51.  *---------------------------------------------------------------------------*/
  52.  
  53. #include "hdf.h"
  54. #include "herr.h"
  55. #include "hfile.h"
  56.  
  57. static uint8 *Dilist=NULL;      /* buffer for list of tag/refs
  58.                                   constituting group */
  59. static int32 Dinlist;           /* no of tag/refs in list */
  60. static int32 Ndi;               /* current position in list */
  61.  
  62. /*-----------------------------------------------------------------------------
  63.  * Name:    DFdiread
  64.  * Purpose: Read a list of DIs into memory
  65.  * Inputs:  file_id: HDF file pointer
  66.  *          tag, ref: id of group which is to be read in
  67.  * Returns: 0 on success, -1 on failure with error set
  68.  * Users:   HDF systems programmers, DF8getrig, other routines
  69.  * Invokes: HDvalidfid, DFIfind, DFgetelement
  70.  * Remarks: assumes tag is a group
  71.  *---------------------------------------------------------------------------*/
  72.  
  73. #ifdef PROTOTYPE
  74. int DFdiread(int32 file_id, uint16 tag, uint16 ref)
  75. #else
  76. int DFdiread(file_id, tag, ref)
  77.     int32 file_id;
  78.     uint16 tag, ref;           /* tag, ref of group */
  79. #endif
  80. {
  81.     char *FUNC="DFdiread";
  82.     int32 length;
  83.  
  84.     HEclear();
  85.  
  86.     if (!HDvalidfid(file_id)) {
  87.        HERROR(DFE_ARGS);
  88.        return FAIL;
  89.     }
  90.  
  91.     /* Find the group. */
  92.     length = Hlength(file_id, tag, ref);
  93.     if (length == FAIL) {
  94.        return FAIL;
  95.     }
  96.     if (Dilist) HDfreespace((VOIDP)Dilist); /* ensure earlier allocs freed */
  97.     Dilist = (uint8 *) HDgetspace((uint32)length);
  98.     if (!Dilist) {
  99.         HERROR(DFE_NOSPACE);
  100.         return FAIL;
  101.     }
  102.  
  103.     Dinlist = length / 4;      /* 4==sizeof DFdi */
  104.     Ndi = 0;                   /* no DIs returned so far */
  105.  
  106.     /* read in group */
  107.     if (Hgetelement(file_id, tag, ref, (uint8 *)Dilist)<0) {
  108.         HDfreespace(Dilist);
  109.         Dilist = NULL;         /* flag value */
  110.         return FAIL;
  111.     }
  112.     return SUCCEED;
  113. }
  114.  
  115. /*-----------------------------------------------------------------------------
  116.  * Name:    DFdiget
  117.  * Purpose: return next DI from list
  118.  * Inputs:  di: space to return DI
  119.  * Returns: 0 on success, -1 on failure with error set
  120.  * Users:   HDF systems programmers, DF8getrig, other routines
  121.  * Invokes: none
  122.  * Remarks: frees Dilist space when all DIs returned
  123.  *---------------------------------------------------------------------------*/
  124.  
  125. #ifdef PROTOTYPE
  126. int DFdiget(uint16 *ptag, uint16 *pref)
  127. #else
  128. int DFdiget(ptag, pref)
  129.     uint16 *ptag;
  130.     uint16 *pref;
  131. #endif
  132. {
  133.     char *FUNC="DFdiget";
  134.     uint8 *p;
  135.     if (Ndi>=Dinlist) {
  136.        return FAIL;
  137.     }
  138.  
  139.     /* compute address of Ndi'th di */
  140.     p = (uint8 *) Dilist + 4 * Ndi;
  141.     Ndi++;
  142.     UINT16DECODE(p, *ptag);
  143.     UINT16DECODE(p, *pref);
  144.  
  145.     if (Ndi==Dinlist) {
  146.         HDfreespace(Dilist);          /* if all returned, free storage */
  147.         Dilist = NULL;         /* flag value */
  148.     }
  149.     return SUCCEED;
  150. }
  151.  
  152.  
  153. /*-----------------------------------------------------------------------------
  154.  * Name:    DFdisetup
  155.  * Purpose: setup space for storing a list of DIs to be written out
  156.  * Inputs:  maxsize: maximum number of DIs expected in the list
  157.  * Returns: 0 on success, -1 on failure with error set
  158.  * Users:   HDF systems programmers, DF8putrig, other routines
  159.  * Invokes: none
  160.  * Remarks: This call should go away sometime.  Need better way to allocate
  161.  *          space, possibly just use a big block of static space
  162.  *---------------------------------------------------------------------------*/
  163.  
  164. #ifdef PROTOTYPE
  165. int DFdisetup(int maxsize)
  166. #else
  167. int DFdisetup(maxsize)
  168.     int maxsize;
  169. #endif
  170. {
  171.     char *FUNC="DFdisetup";
  172.     if (Dilist) HDfreespace(Dilist);
  173.     Dilist = (uint8 *) HDgetspace((uint32)(maxsize * 4));
  174.                                /* 4==sizeof(DFdi) */
  175.     if (!Dilist) {
  176.         HERROR(DFE_NOSPACE);
  177.         return FAIL;
  178.     }
  179.     Dinlist = maxsize;         /* maximum size of list */
  180.     Ndi = 0;                    /* current size of list */
  181.     return SUCCEED;
  182. }
  183.  
  184. /*-----------------------------------------------------------------------------
  185.  * Name:    DFdiput
  186.  * Purpose: add a DI to the list to be written out
  187.  * Inputs:  tag, ref: DI to add
  188.  * Returns: 0 on success, -1 on failure with error set
  189.  * Users:   HDF systems programmers, DF8putrig, other routines
  190.  * Invokes: none
  191.  * Remarks: arg is tag/ref rather than DI for convenience
  192.  *---------------------------------------------------------------------------*/
  193.  
  194. #ifdef PROTOTYPE
  195. int DFdiput(uint16 tag, uint16 ref)
  196. #else
  197. int DFdiput(tag, ref)
  198.     uint16 tag, ref;
  199. #endif
  200. {
  201.     char *FUNC="DFdiput";
  202.     uint8 *p;
  203.  
  204.     if (Ndi>=Dinlist) {
  205.         return FAIL;
  206.     }
  207.  
  208.     /* compute address of Ndi'th di to put tag/ref in */
  209.     p = (uint8 *) Dilist + 4 * Ndi++;
  210.     UINT16ENCODE(p, tag);
  211.     UINT16ENCODE(p, ref);
  212.  
  213.     return SUCCEED;
  214. }
  215.  
  216. /*-----------------------------------------------------------------------------
  217.  * Name:    DFdiwrite
  218.  * Purpose: Write DI list out to HDF file
  219.  * Inputs:  file_id: HDF file pointer
  220.  *          tag, ref: tag and ref of group whose contents is the list
  221.  * Returns: 0 on success, -1 on failure with error set
  222.  * Users:   HDF systems programmers, DF8putrig, other routines
  223.  * Invokes: none
  224.  * Remarks: frees storage for Dilist
  225.  *---------------------------------------------------------------------------*/
  226.  
  227. #ifdef PROTOTYPE
  228. int DFdiwrite(int32 file_id, uint16 tag, uint16 ref)
  229. #else
  230. int DFdiwrite(file_id, tag, ref)
  231.     int32 file_id;
  232.     uint16 tag, ref;
  233. #endif
  234. {
  235.     char *FUNC="DFdiwrite";
  236.     int ret;                   /* return value */
  237.  
  238.     if (!HDvalidfid(file_id)) {
  239.        HERROR(DFE_ARGS);
  240.         return FAIL;
  241.     }
  242.  
  243.     ret = Hputelement(file_id, tag, ref, Dilist, (int32)Ndi*4);
  244.                                /* 4==sizeof(DFdi) */
  245.     HDfreespace(Dilist);
  246.     Dilist = NULL;             /* flag value */
  247.     Dinlist = Ndi = 0;
  248.     return ret;
  249. }
  250.  
  251.